﻿using System;
using System.Collections;
using System.Collections.Generic;
using AutoChess;
using UnityEngine;
namespace AutoChess
{
    
    /// <summary>
    /// 对于自走棋的实体管理接口
    /// </summary>
    class AutoChessPlayerCtr
    {
        #region ins
        private static AutoChessPlayerCtr ins;
        public static AutoChessPlayerCtr Ins
        {
            get
            {
                if (ins == null)
                {
                    ins = new AutoChessPlayerCtr();
                }
                return ins;
            }
        }
        #endregion

        /// <summary> 对战敌人 </summary>
        private PlayerEntity enemyPlayer;
        public PlayerEntity EnemyPlayer
        {
            get { return enemyPlayer;  }
            set { enemyPlayer = value; }
        }

        /// <summary> 主界面的玩家 </summary>
        private PlayerEntity currentPlayer;
        public PlayerEntity CurrentPlayer
        {
            get
            {
                return currentPlayer;
            }
        }
        
        /// <summary>
        /// 获取指定站位上的格子的棋子Id
        /// </summary>
        /// <param name="index">1-12 的战场位置</param>
        /// <returns></returns>
        public int GetFieldGridChessId(FightCampEnum camp,int index)
        {
            if (camp == FightCampEnum.Left)
            {
                return currentPlayer.GetChessIdByFightGridIndex(index);
            }
            else
            {
                return enemyPlayer.GetChessIdByFightGridIndex(index);
            }
        }
        
        /// <summary>
        /// 获得星数
        /// </summary>
        public int GetChessStarNum(FightCampEnum fightCampEnum, int pos)
        {
            int chessId = GetFieldGridChessId(fightCampEnum, pos);
            if (chessId < 0)
            {
                ExLog.LogError("自走棋对战数据中取不到对应棋子！战斗势力：" + fightCampEnum + " 站位：" + pos);
            }
            var chessTable = Table.Blo.AutoChessBlo.GetChessTable(chessId);
            if (chessTable == null)
                return 0;

            return chessTable.star;
        }
        public void DelateCurrentPlayer(bool delateShip = true)
        {
            if (currentPlayer != null)
            {
                currentPlayer.LeaveBattleField(delateShip);
                currentPlayer = null;
            }
        }
        public void DelateEnemyPlayer(bool delateShip = true)
        {
            if (enemyPlayer != null)
            {
                enemyPlayer.LeaveBattleField(delateShip);
                enemyPlayer = null;
            }
        }
        /// <summary>
        /// 添加一整个的当前数据( 观战的时候先把当前玩家的数据推进来 )
        ///  //TODO 数据应该是以包的形式过来 然后创建成Unit
        /// </summary>
        public void GeneratePlayerEntity(PlayerEntity player)
        {
            currentPlayer = player;
        }

        public void Release()
        {
            currentPlayer.LeaveBattleField();
            currentPlayer = null;
            if (enemyPlayer != null)
            {
                enemyPlayer.LeaveBattleField();
                enemyPlayer = null;
            }
        }

        /// <summary>
        /// 哪个位置哪个棋子
        /// </summary>
        public void AddHeroUnit(HeroData heroData)
        {
            currentPlayer.AddHeroUnit(CreateHeroUnit(heroData));
        }

        public void DeleteHeroUnit(ACHero hero)
        {
            if (hero.OwnerPlayerUID == currentPlayer.playerGuid)
            {
                currentPlayer.RemoveHeroUnit(hero);
            }
            else if (hero.OwnerPlayerUID == enemyPlayer.playerGuid)
            {
                enemyPlayer.RemoveHeroUnit(hero);
            }
            else
            {
                BLogger.Error("删除HeroUnit异常!");
            }
        }

        /// <summary>
        /// 创建一个新的英雄
        /// </summary>
        /// <returns></returns>
        private ACHero CreateHeroUnit(HeroData heroData)
        {
            var acHero = ActorManager.Instance.CreateACHero(heroData);
            return acHero;
        }

        #region 构建逻辑实体

        /// <summary>
        /// 进入一个玩家的战场,先注销当前的战场的玩家的逻辑实体，构建新的逻辑实体链接到战场
        /// </summary>
        public PlayerEntity CreatePlayerData(ACPlayerData player)
        {
            PlayerEntity entity = new PlayerEntity();
            entity.playerGuid = player.GUID;

            //TODO 
            HeroData heroData = null;
            ACGrid gridUnit = null;
            foreach (var item in player.herosFight)
            {
                heroData = item.Value;
                gridUnit = AutoChessMapMgr.Ins.GetGridPos(GridType.FightGrid,item.Value.gridIndex);
                if (gridUnit == null)
                {
                    Debug.LogError(string.Format("<color=red>{0}, {1} ,{2} 出生格子空 </color>", entity.playerGuid, GridType.FightGrid, item.Value.gridIndex));
                }
                var heroUnit = CreateHeroUnit(heroData);
                entity.AddHeroUnit(heroUnit);
            }

            foreach (var item in player.herosWait)
            {
                heroData = item.Value;
                gridUnit = AutoChessMapMgr.Ins.GetGridPos(GridType.PreChooseGrid, item.Value.gridIndex);
                var heroUnit = CreateHeroUnit(heroData);
                entity.AddHeroUnit(heroUnit);
            }
            foreach (var item in player.herosShop)
            {
                heroData = item.Value;
                gridUnit = AutoChessMapMgr.Ins.GetGridPos(GridType.ShopGrid, item.Value.gridIndex);
                var heroUnit = CreateHeroUnit(heroData);
                entity.AddHeroUnit(heroUnit);
            }
            if (player.ShipId == 0)
            {
                player.ShipId = Table.DataModel.TableConfig.autochessDefaultShip; 
            }

            entity.AddShip(CreateShipUnit(player.ShipId, AutoChessFieldViewCtr.Ins.SceneConfig.selfPos.position, entity));

            return entity;
        }

        public void FightGridHeroBorn(ACPlayerData data , PlayerEntity entity)
        {
            foreach (var item in data.herosFight)
            {
                CreateHeroUnit(item.Value, entity);
            }
        }

        public ACHero CreateHeroUnit(HeroData heroData,PlayerEntity entity)
        {
            ACGrid gridUnit = AutoChessMapMgr.Ins.GetGridPos(heroData.gridType, heroData.gridIndex);
            if (gridUnit == null)
            {
                Debug.LogError("Errer");
            }
            var heroUnit = CreateHeroUnit(heroData);
            entity.AddHeroUnit(heroUnit);
            return heroUnit;
        }
        public PlayerEntity CreateEnemyEntityPlayer(ACPlayerData enemy)
        {
            PlayerEntity entity = new PlayerEntity();
            HeroData heroData = null;
            ACGrid gridUnit = null;
            foreach (var item in enemy.herosFight)
            {
                heroData = item.Value;
                gridUnit = AutoChessMapMgr.Ins.GetGridPos(GridType.EnemyGrid, item.Value.gridIndex);
                if (gridUnit == null)
                {
                    Debug.LogError(string.Format("<color=red>{0}, {1} ,{2} 出生格子空 </color>", entity.playerGuid, GridType.EnemyGrid, item.Value.gridIndex));
                }
                var heroUnit = CreateHeroUnit(heroData);
                entity.AddHeroUnit(heroUnit);
            }
           
            return entity;
        }

        public ShipUnit CreateShipUnit(int shipId, Vector3 shipPos,PlayerEntity player)
        {
            ShipUnit shipUnit = new ShipUnit(player);
            shipUnit.ShipId = shipId;
            shipUnit.Position = shipPos;
            shipUnit.ConnectRenderer(AutoChessFieldViewCtr.Ins.GetShipRenderer());
            return shipUnit;
        }
        #endregion
    }
}